package com.lancoo.ccas52.arrangecourse.AlgDataSource.dataprocess.Impl;

import com.lancoo.ccas52.arrangecourse.AlgDataSource.baseentity.College;
import com.lancoo.ccas52.arrangecourse.AlgDataSource.baseentity.Course;
import com.lancoo.ccas52.arrangecourse.AlgDataSource.baseentity.Teacher;
import com.lancoo.ccas52.arrangecourse.AlgDataSource.common.Constants;
import com.lancoo.ccas52.arrangecourse.AlgDataSource.dataprocess.CourseDataService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * @Description 课程数据生成
 * @Author Fortysun
 * @Date 2023/4/12 14:22
 **/
public class CourseDataServiceImpl implements CourseDataService {
    private HashMap<String, List<Teacher>> teacherMap;
    private HashMap<String, List<Course>> courseMap;

    public CourseDataServiceImpl(List<College> collegeList) {

        teacherMap = initTeacherMap(collegeList);
        courseMap = initCourseMap(collegeList, teacherMap);
    }
    /**
     * 初始化课程列表
     * @return
     */
    @Override
    public HashMap<String, List<Course>> getCourseMap() {
        return this.courseMap;
    }

    /**
     * 初始化课程列表
     * @return
     */
    @Override
    public HashMap<String, List<Teacher>> getTeacherMap() {
        return this.teacherMap;
    }





    /**
     * 初始化教师数据
     * @param collegeList
     * @return
     */
    private HashMap<String, List<Teacher>> initTeacherMap(List<College> collegeList) {

        HashMap<String, List<Teacher>> teacherMap = new HashMap<>();
        int j = 0;
        for (College college : collegeList) {
            List<Teacher> teacherList = new ArrayList<>();
            for (int i = 0; i < Constants.COLLEGE_TEACHER_NUM; i++) {
                teacherList.add(new Teacher(
                        (long)(j* Constants.COLLEGE_TEACHER_NUM + i+1),
                        college.getColleageName() + "-老师" + (i+1),
                        college.getCollegeId()
                ));

            }
            teacherMap.put(college.getCollegeId(), teacherList);
            j++;
        }

        return teacherMap;
    }



    /**
     * 初始化课程列表
     * @param collegeList 基础学院信息列表
     * @param teacherMap 教师信息
     * @return
     */
    private HashMap<String, List<Course>> initCourseMap(List<College> collegeList,
                                                        HashMap<String, List<Teacher>> teacherMap) {
        HashMap<String, List<Course>> courseMap = new HashMap<>();
        Long j = 0L;
        for (College college : collegeList) {
            List<Course> courseList = new ArrayList<>();
            //处理指定学院的教师列表
            List<Teacher> allTeacherList = teacherMap.get(college.getCollegeId());
            for (int i = 0; i < Constants.COLLEGE_COURSE_NUM; i++) {
                Long courseNo = ((i+1)*(j+1));
                courseList.add(new Course(
                        courseNo,
                        "课程"+ courseNo,
                        Constants.COURSE_TYPE_NUM,
                        Constants.WEEK_CLASS_HOUR_NUM,
                        initWeeks(Constants.TERM_WEEK_NUM, i%4),
                        college.getCollegeId(),
                        1,
                        getTeacherList(i, allTeacherList)
                        ));
            }
            courseMap.put(college.getCollegeId(), courseList);
            j++;
        }

        return courseMap;
    }

    /**
     * 获取分配给指定课程的，教师id字符串，多个以逗号分隔
     * @param index 遍历课程列表时的索引信息
     * @param allTeacherList 学院的所有教师列表
     * @return
     */
    private List<Teacher> getTeacherList(int index, List<Teacher> allTeacherList) {
        List<Teacher> teacherList = new ArrayList<>();


        //如果分配的教师数量，比课程数量多， 则每个课程要分配多个教师
        if (Constants.COLLEGE_TEACHER_NUM > Constants.COLLEGE_COURSE_NUM) {
            //每个课程需要分配的教师数量
            // 学院课程数除以学院的教师数量，120个名教师，50个课程，这个值就是2
            int courseTeacherNum = Constants.COLLEGE_TEACHER_NUM/ Constants.COLLEGE_COURSE_NUM;
            //不能整除的话，剩下的余数，120个名教师，50个课程，这个值就是120-50*2=20
            // 不能整除的情况，需要将教师均匀的分配给每个课程，120个名教师，50个课程，那么前面20个课程分配3名教师
            //后面30节课分配2名教师
            int dValue = Constants.COLLEGE_TEACHER_NUM - courseTeacherNum * Constants.COLLEGE_COURSE_NUM;

            if (index < dValue) {
                courseTeacherNum ++;
                //前面20个课程分配3个教师
                for (int j = 0; j < courseTeacherNum; j++) {
                    teacherList.add(allTeacherList.get((index*courseTeacherNum+ j) % Constants.COLLEGE_TEACHER_NUM));
                }
            } else {
                //后面30个课程，分配2名教师
                for (int j = 0; j < courseTeacherNum; j++) {
                    teacherList.add(allTeacherList.get((index*courseTeacherNum + j) % Constants.COLLEGE_TEACHER_NUM));
                }
            }

        } else {
            //教师数量小于等于课程数量，每个课程就分配一个教师
            teacherList.add(allTeacherList.get(index% Constants.COLLEGE_TEACHER_NUM));
        }

        return teacherList;
    }


    /**
     * 生成周次信息
     * @param termWeeks 学期的总周数
     * @param type 类型：0全部、1单周、2双周、3自定义
     * @return
     */
    private String initWeeks(int termWeeks, int type) {
        String weeks = "";
        switch (type) {
            case 0://全部周次
                weeks = allWeeks(termWeeks);
                break;
            case 1://单周
                weeks = oddWeeks(termWeeks);
                break;
            case 2://双周
                weeks = evenWeeks(termWeeks);
                break;
            case 3://自定义周次
                weeks = customWeeks(termWeeks);
                break;

            default:
                System.out.println("初始化周次出错~");
                break;
        }

        return weeks;
    }

    /**
     * 生成全部周次信息，默认都是1
     * @param termWeeks 学期的总周数
     * @return
     */
    private String allWeeks(int termWeeks) {
        String retStr = "";
        for (int i = 0; i < termWeeks; i++) {
            retStr += "1";
        }
        return retStr;
    }

    /**
     * 生成单周信息，10101010...
     * @param termWeeks 学期的总周数
     * @return
     */
    private String oddWeeks(int termWeeks) {
        String retStr = "";
        for (int i = 0; i < termWeeks; i++) {
            if (i / 2 == 0) {
                retStr += "1";
            } else {
                retStr += "0";
            }

        }
        return retStr;
    }

    /**
     * 生成双周信息，01010101...
     * @param termWeeks 学期的总周数
     * @return
     */
    private String evenWeeks(int termWeeks) {
        String retStr = "";
        for (int i = 0; i < termWeeks; i++) {
            if (i / 2 == 0) {
                retStr += "0";
            } else {
                retStr += "1";
            }

        }
        return retStr;
    }

    /**
     * 自定义周次，字符串随机
     * @param termWeeks 学期的总周数
     * @return
     */
    private String customWeeks(int termWeeks) {
        String retStr = "";
        Random rnd = new Random();
        for (int i = 0; i < termWeeks; i++) {
            retStr+=rnd.nextInt(2);
        }
        return retStr;
    }
}
